package com.misoo.ex11;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class Ex11Activity extends Activity implements OnCheckedChangeListener{
	private final int WC = RadioGroup.LayoutParams.WRAP_CONTENT;
	private RadioGroup rg_layout;
	private DrawView dv;
	private LinearLayout layout;
	private int mColor = Color.YELLOW;
	private RadioGroup.LayoutParams params;
	private LinearLayout.LayoutParams para;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        this.show_layout_01();
    }
    
    public void show_layout_01(){
    	layout = new LinearLayout(this);
    	layout.setOrientation(LinearLayout.VERTICAL);
    	para = new LinearLayout.LayoutParams(230,140);
    	dv = new DrawView(this);
    	layout.addView(dv,para);
    	
    	Button btn = new Button(this);
    	Drawable dw;
    	if(mColor == Color.BLUE)
    		dw = this.getResources().getDrawable(R.drawable.change_to_yellow);
    	else
    		dw = this.getResources().getDrawable(R.drawable.change_to_blue);
    	btn.setText("Change Color");
    	btn.setBackgroundDrawable(dw);
    	para = new LinearLayout.LayoutParams(WC,WC);
    	btn.setOnClickListener(listener);
    	layout.addView(btn,para);
    	
    	rg_layout = new RadioGroup(this);
    	params = new RadioGroup.LayoutParams(WC,WC);
    	rg_layout.setOrientation(RadioGroup.VERTICAL);
    	rg_layout.setLayoutParams(params);
    	rg_layout.setOnCheckedChangeListener(this);
    	
    	RadioButton button1 = new RadioButton(this);
    	button1.setText("Yellow");
    	button1.setId(1001);
    	params = new RadioGroup.LayoutParams(WC,WC);
    	rg_layout.addView(button1, params);
    	
    	RadioButton button2 = new RadioButton(this);
    	button2.setText("Blue");
    	button2.setId(1002);
    	params = new RadioGroup.LayoutParams(WC,WC);
    	rg_layout.addView(button2,params);
    	
    	setContentView(layout);
    }
    
    public int getColor(){
    	return mColor;
    }
    
    private OnClickListener listener = new OnClickListener(){
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
    		setContentView(rg_layout);			
		}
    };
    
    /* (non-Javadoc)
	 * @see android.widget.RadioGroup.OnCheckedChangeListener#onCheckedChanged(android.widget.RadioGroup, int)
	 */
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub
    	if(group.getCheckedRadioButtonId()==1001)
    		mColor = Color.YELLOW;
    	else
    		mColor = Color.BLUE;
    	this.show_layout_01();
	}

}